package com.eleaf.adbb.template;

import java.lang.reflect.Modifier;

import com.eleaf.adbb.util.BaseUtil;

public class VariableTemplate {

	private String modifiers, variableName, variableType, packagePath;
	private String annotation = "\n";

	/**
	 * 初始化变量模版
	 * 
	 * @param modifiers 修饰符
	 * @param variableName 变量名
	 * @param packagePath 变量类型的包路径
	 * @param variableType 变量类型
	 */
	public VariableTemplate( int modifiers, String variableName, String packagePath, String variableType ) {
		this.modifiers = Modifier.toString ( modifiers );
		this.variableName = variableName;
		this.packagePath = packagePath;
		this.variableType = variableType;
	}

	/**
	 * 初始化变量模版
	 * 
	 * @param modifiers 修饰符
	 * @param variableName 变量名
	 * @param variableType 变量类型
	 */
	public VariableTemplate( int modifiers, String variableName, Class<?> variableType ) {
		this.modifiers = Modifier.toString ( modifiers );
		this.variableName = variableName;
		this.variableType = variableType.getSimpleName ();

		if ( !variableType.isPrimitive () ) {
			if ( variableType.isArray () && variableType.getComponentType ().isPrimitive () ) {
				return;
			}
			this.packagePath = variableType.getCanonicalName ();
		}
	}

	/**
	 * 添加Annotation
	 * 
	 * @param annotations
	 */
	public void addAnnotation( String... annotations ) {
		for ( String line : annotations ) {
			annotation = BaseUtil.appendString ( annotation, line, "\n" );
		}
	}

	@Override
	public String toString() {
		return BaseUtil.appendString ( annotation, modifiers, " ", variableType, " ", variableName, ";" );
	}

	/**
	 * 获取变量类型的包路径
	 * 
	 * @return
	 */
	public String getPackagePath() {
		return packagePath;
	}

	public String getVariableName() {
		return variableName;
	}

	public String getVariableType() {
		return variableType;
	}
}
